package com.xm.xmapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.android.volley.VolleyError;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.CustomLayout;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.Get_order_infoRequest;
import com.xm.xmapp.request.bean.Get_user_money_infoRequest;
import com.xm.xmapp.response.bean.BaseBeanResponse;
import com.xm.xmapp.response.bean.ResponsePileOrderDetail;
import com.xm.xmapp.response.bean.ResponseUserMoney;
import com.xm.xmapp.tools.StringTools;
import com.xm.xmapp.tools.Tools;
import com.xm.xmapp.wxapi.WXPayEntryActivity;

public class PileOrderDetailActivity extends BaseActivity implements OnClickListener, OnRefreshListener {

	private CustomLayout orderMoney, chargeMoney, serverMoney, chargeTime, chargeCount, orderTime, orderNumber,
			orderPayType, carrieroperator, chargeAddress, chargeNumber, chargeType, chargeOrder, endTime;

	private LinearLayout status_layout, pay;
	private SwipeRefreshLayout id_swipe_ly;
	private TextView go_pay, error_info;
	private String orderId;
	private ResponseUserMoney mUserMoneyBean;
	private ResponsePileOrderDetail pileOrderDetailBean;

	/**
	 * 订单变为已支付订单后，重新拉去数据
	 */
	@Override
	public Object dataChang(int flag, Object data) {

		switch (flag) {
		case Type.A_PileOrderDetail.DEFAULT:
			setStatus(false);
			break;

		default:
			break;
		}

		return super.dataChang(flag, data);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pileorderdetail);
	}

	@Override
	public void initView() {
		orderMoney = (CustomLayout) findViewById(R.id.orderMoney);
		chargeMoney = (CustomLayout) findViewById(R.id.chargeMoney);
		serverMoney = (CustomLayout) findViewById(R.id.serverMoney);
		chargeTime = (CustomLayout) findViewById(R.id.chargeTime);
		chargeCount = (CustomLayout) findViewById(R.id.chargeCount);
		orderTime = (CustomLayout) findViewById(R.id.orderTime);
		orderNumber = (CustomLayout) findViewById(R.id.orderNumber);
		orderPayType = (CustomLayout) findViewById(R.id.orderPayType);
		carrieroperator = (CustomLayout) findViewById(R.id.carrieroperator);
		chargeAddress = (CustomLayout) findViewById(R.id.chargeAddress);
		chargeNumber = (CustomLayout) findViewById(R.id.chargeNumber);
		chargeType = (CustomLayout) findViewById(R.id.chargeType);
		chargeOrder = (CustomLayout) findViewById(R.id.chargeOrder);
		endTime = (CustomLayout) findViewById(R.id.endTime);
		pay = (LinearLayout) findViewById(R.id.pay);
		go_pay = (TextView) findViewById(R.id.go_pay);
		status_layout = (LinearLayout) findViewById(R.id.status_layout);
		id_swipe_ly = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
		error_info = (TextView) findViewById(R.id.error_info);
	}

	@Override
	public void initData() {
		setMyTitle("订单详情");
		boolean status = getIntent().getBooleanExtra("status", false);// 是否需要支付
		setStatus(status);

	}

	/**
	 * 设置订单状态
	 * 
	 * @param status
	 */
	private void setStatus(boolean status) {
		orderId = getIntent().getStringExtra("orderId");
		chargeOrder.setText2(orderId);
		go_pay.setBackgroundResource(R.drawable.myborder);
		if (status) {
			getMoney();
			pay.setVisibility(View.VISIBLE);
			status_layout.setVisibility(View.GONE);
			chargeOrder.setVisibility(View.VISIBLE);
		} else {
			chargeOrder.setVisibility(View.GONE);
			pay.setVisibility(View.GONE);
			status_layout.setVisibility(View.VISIBLE);
		}
		getData(orderId);
		id_swipe_ly.setOnRefreshListener(this);
	}

	private void getMoney() {
		Get_user_money_infoRequest requestBean = new Get_user_money_infoRequest();
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				mUserMoneyBean = JSON.parseObject(response, ResponseUserMoney.class);
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});
	}

	private void getData(String orderId) {
		Get_order_infoRequest requestBean = new Get_order_infoRequest();
		requestBean.orderNo = orderId;
		GetDataInterFace<String> listener = new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				id_swipe_ly.setRefreshing(false);

				BaseBeanResponse<String> base = JSON.parseObject(response,
						new TypeReference<BaseBeanResponse<String>>() {
						});

				switch (base.errCode) {
				case 0:
					pileOrderDetailBean = JSON.parseObject(base.data, ResponsePileOrderDetail.class);
					orderMoney.setText2(pileOrderDetailBean.charging_total_money + "元");
					chargeMoney.setText2(pileOrderDetailBean.charging_total_elec_money + "元");
					serverMoney.setText2(pileOrderDetailBean.charging_total_sevice_money + "元");
					chargeTime.setText2(StringTools.getTime(pileOrderDetailBean.charging_time / 60));
					chargeCount.setText2(pileOrderDetailBean.charging_total_power + "度");
					orderTime.setText2(StringTools.getTime(pileOrderDetailBean.charging_order_start_time * 1000,
							"yyyy-MM-dd  HH:mm:ss"));
					orderNumber.setText2(pileOrderDetailBean.charging_order_no);
					endTime.setText2(StringTools.getTime(pileOrderDetailBean.charging_order_end_time * 1000,
							"yyyy-MM-dd  HH:mm:ss"));				
					orderPayType.setText2(pileOrderDetailBean.pay_method);
					carrieroperator.setText2(pileOrderDetailBean.charging_order_equipment_sort);
					chargeAddress.setText2(pileOrderDetailBean.charging_pile_network_name);
					chargeNumber.setText2(pileOrderDetailBean.charging_pile_interface_number);
					chargeType.setText2(pileOrderDetailBean.charging_fast_slow == 1 ? "快充" : "慢充");
					error_info.setVisibility(View.GONE);
					go_pay.setBackgroundResource(R.drawable.onclickbackground1);
					go_pay.setOnClickListener(PileOrderDetailActivity.this);
					break;
				case 50003:
					go_pay.setBackgroundResource(R.drawable.myborder);
					error_info.setVisibility(View.VISIBLE);
					error_info.setText(base.errMsg);
					go_pay.setOnClickListener(null);
					break;

				default:
					Tools.showMessage(base.errMsg);
					break;
				}

			}
		};

		MyAppcation.getMyAppcation().getPostData(this, requestBean, listener, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
				id_swipe_ly.setRefreshing(false);
			}
		});

	}

	@Override
	public void initEvent() {

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.go_pay:
			if (mUserMoneyBean != null) {
				go_to_pay();
			} else {
				getMoney();
			}
			break;

		default:
			break;
		}

	}

	private void go_to_pay() {

		if (pileOrderDetailBean != null) {
			WXPayEntryActivity.currentStatus = WXPayEntryActivity.PILE;
			intent = new Intent(getApplicationContext(), PileOrderPayActivity.class);
			intent.putExtra("pileOrderDetailBean", pileOrderDetailBean);
			intent.putExtra("mUserMoneyBean", mUserMoneyBean);
			startActivity(intent);
		} else {
			getData(orderId);
		}

	}

	@Override
	public void onRefresh() {
		promptDialog.show();
		getData(orderId);
	}
}
